// pages/productbrands/productBrands.js
import {
  brands
} from '../../api.js'
let app = getApp();
Page({
  data: {
    type: '', //全网 1； 商品库存：0
    brandList: null,
    letterArray: [
      "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z"
    ]
  },

  onLoad: function(options) {
    console.log('options', options)
    brands({
      token: app.globalData.token
    }).then(res => {
      if (res.data.code == 666) { 
        let data = res.data.data;
        this.setData({
          brandList: data,
          type: options.type
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }

    }).catch(err => {
      console.log(err)
    })
  },
  onUnload: function() {
    if (this.data.type == 0) {
      wx.setStorageSync('controlType', 'goodControl')
    } else if (this.data.type == 1) {
      wx.setStorageSync('controlType', 'allGoodControl')
    }
  },
  scrollTo(e) {
    let id = e.target.dataset.id
    this.setData({
      "target": id
    })
  },
  brandsSkip(e) {
    //  wx.setStorageSync('brands',e.currentTarget.dataset.item)
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    console.log(prevPage)
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    if (this.data.type == 0) {
      wx.setStorageSync('controlType', 'goodControl')
      prevPage.setData({
        'submitInfo.brand': e.currentTarget.dataset.item,
        selected1:3
      })
    } else if (this.data.type == 1) {
      wx.setStorageSync('controlType', 'allGoodControl')
      prevPage.setData({
        'allSubmitInfo.brand_name': e.currentTarget.dataset.item,
        selected2: 5
      })
    }

    wx.navigateBack({
      delta: 1,
    })
  },
})